<?php
namespace App\Http\Controllers\Backstage;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;

use Illuminate\Support\Facades\DB;
use App\Models\CommonModel;
use App\Models\BackstageModel;

class BCommonController extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
    protected $table;
    public function __construct(){
        // echo 123;exit;
        // $url = $request->url();
        // dump($url);exit;
        // $arr = explode('/',$url);
        // $action = end($arr);
        // 獲取路由信息
        $action = \Route::currentRouteAction();
        list($controller, $method) = explode('@', $action);
        $controller = substr(strrchr($controller,'\\'),1);
        $class = str_replace("controller",'',strtolower($controller));
        $uri = $class.'/'.$method;
        $type_id = CommonModel::getField('menu','id',['url'=>$uri]);
        if($type_id==0){
            $type_id = CommonModel::getField('menu','id',['action'=>$class]);
        }
        $this->table=$class;
        // 獲取導航
        // $menu = BackstageModel::getMenu();
        if(!empty($type_id)){
            $guide = BackstageModel::getMenuById($type_id);
        }else{
            $guide = '';
        }
        // dd($class);
        // dd($guide);
        view()->share(['type_id'=>$type_id,'guide'=>$guide,'class'=>$class,'method'=> $method,'update_sort'=>'/backstage/'.$class.'/update_sort','doadd'=>'/backstage/'.$class.'/doadd','doedit'=>'/backstage/'.$class.'/doedit','dodel'=>'/backstage/'.$class.'/dodel']);
    }
    //更新排序
    public function update_sort(Request $request){
        if($_POST){
            if(isset($_POST['id'])){
                $id=$_POST['id'];
                if(isset($_POST['sort']) && $_POST['sort']!=''){
                    $editnum = DB::table($this->table)->where('id',$id)->update(['sort_order' => $_POST['sort']]);
                    if($editnum){
                        $res['status'] = 1;
                        $res['message'] = "修改成功";
                        $res['id'] = $id;
                    }else{
                        $res['status'] = 0;
                        $res['message'] = "修改失敗";
                    }
                    exit(json_encode($res));
                }else{
                    $res['status'] = 0;
                    $res['message'] = "數據不能為空";
                    exit(json_encode($res));
                }
            }else{
                $res['status'] = 0;
                $res['message'] = "未獲得ID";
                exit(json_encode($res));
            }
            
        }else{
            $res['status'] = 0;
            $res['message'] = "修改失敗";
            exit(json_encode($res));
        }
    }
    
    // 刪除
    public function dodel(Request $request){
        // dd($this->class);
        if($request->isMethod('POST')){
            $idstr=$request->input('idstr');
            if($idstr != ''){
                $i = 0;
                $y = 0;
                $idarr = explode(",",$idstr);
                // dd(count($idarr));
                if(count($idarr)>1){
                    foreach ($idarr as $id) {
                        $res =CommonModel::delData($this->table,['id'=>$id]);
                        if($res){
                            if($this->table=='user'){
                                $logContent = config('app.op_type')[1].'ID为'.$id.'的用户';
                                saveCommonLog($id, $logContent, 'user', session('admin')->real_name, session('admin')->id, 1);
                            }
                            if($this->table=='admin'){
                                $logContent = config('app.op_type')[1].'ID为'.$id.'的管理员';
                                saveCommonLog($id, $logContent, 'admin', session('admin')->real_name, session('admin')->id, 1);
                            }
                            $i++;
                        }else{
                            $y++;
                        }
                    }
                    if($y == 0){
                        
                        return response()->json(array('status' => 1,'message'=>'刪除成功'));
                    }elseif($i == 0){
                        return response()->json(array('status' => 0,'message'=>'刪除失敗'));
                    }else{
                        return response()->json(array('status' => 2,'message'=>'有'.$i.'條刪除成功，有'.$y.'條刪除失敗'));
                    }
                }else{
                    // DB::enableQueryLog();
                    $res =CommonModel::delData($this->table,['id'=>$idarr[0]]);
                    // return response()->json(DB::getQueryLog());
                    // $queries = DB::getQueryLog();
                    // dd($res);
                    if($res){
                        if($this->table=='user'){
                            $logContent = config('app.op_type')[1].'ID为'.$idarr[0].'的用户';
                            saveCommonLog($idarr[0], $logContent, 'user', session('admin')->real_name, session('admin')->id, 1);
                        }
                        if($this->table=='admin'){
                            $logContent = config('app.op_type')[1].'ID为'.$idarr[0].'的管理员';
                            saveCommonLog($idarr[0], $logContent, 'admin', session('admin')->real_name, session('admin')->id, 1);
                        }
                        return response()->json(array('status' => 1,'message'=>'刪除成功'));
                    }else{
                        return response()->json(array('status' => 0,'message'=>'刪除失敗'));
                    }
                }
                
                
            }
        }
    }





}
